import Vue from 'vue'
import Router from 'vue-router'
import { getToken } from '@/utils/auth'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
// import pxBar from '@/components/px/px-bar'
// import pxPie from '@/components/px/px-pie'
// import pxLine from '@/components/px/px-line'
// import flexV from '@/components/px/flex-v'
// import flexH from '@/components/px/flex-h'
const Home = () => import('@/components/home')
const pxLine = () => import('@/components/px/px-line')
const pxBar = () => import('@/components/px/px-bar')
const pxPie = () => import('@/components/px/px-pie')
const pxTabs = () => import('@/components/px/px-tabs')
const flexV = () => import('@/components/px/flex-v')
const flexH = () => import('@/components/px/flex-h')
const music = () => import('@/components/px/music')
const scoped = () => import('@/components/px/scoped')
const html2canvas = () => import('@/components/px/html2canvas')
const Login = () => import('@/components/login/index')

Vue.use(Router)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  { path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '/',
        redirect: '/pxbar'
      },
      {
        path: '/pxbar',
        name: 'pxBar',
        component: pxBar
      },
      {
        path: '/pxpie',
        name: 'pxPie',
        component: pxPie
      },
      {
        path: '/pxline',
        name: 'pxLine',
        component: pxLine
      },
      {
        path: '/pxtabs',
        name: 'pxTabs',
        component: pxTabs
      },
      {
        path: '/flexv',
        name: 'flexv',
        component: flexV
      },
      {
        path: '/flexh',
        name: 'flexh',
        component: flexH
      },
      {
        path: '/music',
        name: 'music',
        component: music
      },
      {
        path: '/scoped',
        name: 'scoped',
        component: scoped
      },
      {
        path: '/html2canvas',
        name: 'html2canvas',
        component: html2canvas
      }
    ]
  }
]

const router = new Router({
  routes
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) { // 存在token
    if (to.path === '/login') { // 当前页是登录直接跳过进入主页
      console.log('to.path === /login')
      next('/home')
    } else {
      console.log('to.path !== /login')
      next('/home')
    }
  } else {
    console.log('no')
    next()
  }
})

router.afterEach((to, from) => {
  document.title = to.name
  NProgress.done()
})

export default router
